﻿using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class DragUGUI : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    public CButton btnBegin;
    public bool xEnable = true;
    public bool yEnable = true;
    public bool usePosLimit = false;
    public int minX = -960;
    public int maxX = 960;
    public int minY = -470;
    public int maxY = 470;
    public Action onStartDrag, onFinishDrag;
    public RectTransform rectTransform { get { return transform as RectTransform; } }
    internal bool isDraging;
    int _fingerId = -1;
    Vector2 _previousPos, _screenPos;
    bool isTriggerByButton;
    Canvas _canvas;
    Canvas canvas { get { return _canvas ?? (_canvas = GetComponentInParent<Canvas>()); } }

    private void Awake()
    {
        if (btnBegin)
        {
            btnBegin.triggerInterval = 0;
            btnBegin.SetPress(() =>
            {
                if (Application.isEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.OSXPlayer)
                {
                    _previousPos = ScreenHelper.ScreenToUGUIPoint(Input.mousePosition, canvas);
                }
                else
                {
                    _previousPos = ScreenHelper.ScreenToUGUIPoint(Input.GetTouch(0).position, canvas);
                }
                isDraging = true;
                isTriggerByButton = true;
                onStartDrag?.Invoke();
            });
        }
    }

    protected virtual void Update()
    {
        if (isDraging)
        {
            if (Application.isEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.OSXPlayer)
            {
                _screenPos = Input.mousePosition;
            }
            else
            {
                for (int i = 0; i < Input.touchCount; i++)
                {
                    Touch touch = Input.GetTouch(i);
                    if (touch.fingerId == _fingerId)
                    {
                        _screenPos = touch.position;
                    }
                }
            }
            Vector2 currPos = ScreenHelper.ScreenToUGUIPoint(_screenPos, canvas);
            Vector2 delta = currPos - _previousPos;
            _previousPos = currPos;
            Vector2 pos = rectTransform.anchoredPosition;
            if (xEnable)
            {
                pos.x += delta.x;
            }
            if (yEnable)
            {
                pos.y += delta.y;
            }
            if (usePosLimit)
            {
                if (pos.x < minX)
                {
                    pos.x = minX;
                }
                if (pos.x > maxX)
                {
                    pos.x = maxX;
                }
                if (pos.y < minY)
                {
                    pos.y = minY;
                }
                if (pos.y > maxY)
                {
                    pos.y = maxY;
                }
            }
            rectTransform.anchoredPosition = pos;
        }

        if (isTriggerByButton)
        {
            if (Input.GetMouseButtonUp(0))
            {
                if (isDraging)
                {
                    isDraging = false;
                    onFinishDrag?.Invoke();
                }
            }
        }
    }

    public virtual void OnPointerDown(PointerEventData eventData)
    {
        if (!btnBegin && _fingerId == -1)
        {
            _previousPos = ScreenHelper.ScreenToUGUIPoint(Input.mousePosition, canvas);
            isDraging = true;
            _fingerId = eventData.pointerId;
            onStartDrag?.Invoke();
        }
    }

    public virtual void OnPointerUp(PointerEventData eventData)
    {
        if (_fingerId == eventData.pointerId)
        {
            _fingerId = -1;
            if (isDraging)
            {
                isDraging = false;
                onFinishDrag?.Invoke();
            }
        }
    }
}